<?php

namespace App\Services\User;

use App\Services\User\Token\TokenLogic;

/**
 * Class User 用户token
 * @package App\Services\User
 */
class Token  {

    /**
     * token 生成
     * @param array $data
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function create(array $data)
    {
        return (new TokenLogic)->create($data);
    }

//
//    // 根据token验证一条权限
//    public function check(): array
//    {
//        $data = $this->haveIssetReceiveData(['token'=>' ', 'auth'=> ' ']);
//        return $this->stepRun($data, 'check', 'check');
//    }

    /**
     * 根据token验证N条权限是否全部通过
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function checkAuths($token, $auths = []): array
    {
        ValidateToken::checkValidate(['token' => $token], ['token'], true, ['requiredToken' => true]);

        if(is_string($auths)) {
            $auths = explode(',', $auths);
        } else if (!is_array($auths)){
            return verify_err($auths, '$path只能是数组或字符串类型');
        }

        return (new TokenLogic())->checkAuths(['token' => $token, 'auths' => $auths]);
    }

    /**
     * 根据token获取权限
     *
     * @param array $data
     * @param string $appendMsg
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function getAuthByToken(array $data, string $appendMsg = ''): array
    {
        ValidateUser::checkValidate($data, ['token']);
        return (new TokenLogic())->getAuthByToken($data);
    }
//
//    // 清理所有token
//    public function clean(): array
//    {
//        return $this->logic($this->tokenLogic)->clean();
//    }
//    // 删除某一个token
//    public function delete(): array
//    {
//        $data = $this->haveIssetReceiveData(['token'=>' ']);
//        return $this->stepRun($data, 'delete', 'delete');
//    }

    /**
     * 获取token保存的内容
     * @param $token
     * @return array
     */
    public function hGetAll($token)
    {
        return (new TokenLogic())->hGetAll($token);
    }
//    //手动给某个token延时“过期时间”
//    public function continueLife(): array
//    {
//        $data = $this->haveIssetReceiveData(['token'=>' ']);
//        return $this->stepRun($data, 'continueLife', '');
//    }
//    // 在某个token保存信息
//    public function hMset(): array
//    {
//        $data = $this->haveIssetReceiveData(['token'=>' ', 'arr'=> [] ]);
//        return $this->stepRun($data, 'hMset', 'hMset');
//    }
//    // 将token中保存的信息置空
//    public function empty(): array
//    {
//        $data = $this->haveIssetReceiveData(['token'=>' ']);
//        return $this->stepRun($data, 'empty', 'empty');
//    }
//    // 是否设置了某一个变量
//    public function hExists(): array
//    {
//        $data = $this->haveIssetReceiveData(['token'=>' ', 'key' =>' ']);
//        return $this->stepRun($data, 'hExists', 'hExists');
//    }
//    // 给一个token增加角色,并返回增加后的角色
//    public function addRoles(): array
//    {
//        $data = $this->haveIssetReceiveData(['token'=>' ', 'roles' =>' ']);
//        return $this->stepRun($data, 'addRoles', 'addRoles');
//    }
//    // 给一个token减去角色,并返回余下的角色
//    public function subRoles(): array
//    {
//        $data = $this->haveIssetReceiveData(['token'=>' ', 'roles' =>' ']);
//        return $this->stepRun($data, 'subRoles', 'addRoles');
//    }
//    // 取得当前token中保存的角色
//    public function getRoles(): array
//    {
//        $data = $this->haveIssetReceiveData(['token'=>' ']);
//        return $this->stepRun($data, 'getRoles', 'getRoles');
//    }
}